package com.qf.conf;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

public class LoadBalancerConfig {

    @Bean
    public ReactorServiceInstanceLoadBalancer loadBalancer(Environment env, LoadBalancerClientFactory factory){

        String serviceId = env.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        ObjectProvider<ServiceInstanceListSupplier> provider = factory.getLazyProvider(serviceId,ServiceInstanceListSupplier.class);

        //获取自定义参数
//        String loadbalancerparam = env.getProperty("loadbalancerparam");
//        System.out.println(loadbalancerparam);
//
//        return new MyLoadBalancer(provider,serviceId,loadbalancerparam);

        return new RandomLoadBalancer(provider,serviceId);
    }
}
